package com.example.electronicmall.base;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.electronicmall.R;
import com.example.electronicmall.adapter.CommodityLeftAdapter;
import com.example.electronicmall.adapter.CommodityRightAdapter;
import com.example.electronicmall.application.CommodityPresenter;
import com.example.electronicmall.bean.CommdityData;
import com.example.electronicmall.bean.CommentBean;
import com.example.electronicmall.bean.LeftModel;
import com.example.electronicmall.bean.RightModel;
import com.example.electronicmall.http.IRequestShopp;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


//商品分类
public class CommodityClassifyActivity extends AppCompatActivity {
    private static final String TAG = CommodityClassifyActivity.class.getSimpleName();
    private IRequestShopp shoppDeanPrestion;


    List<LeftModel> leftModels = new ArrayList<>();
    List<RightModel> rightModels;
    CommodityLeftAdapter adapter;
    CommodityRightAdapter rightAdapter;
    ImageView return_key;
    RecyclerView leftView;
    RecyclerView rightView;
    TextView textView;

    private boolean mIsFromClick = false;


    ImageView im_right;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_classify);

        shoppDeanPrestion = new CommodityPresenter(this);
        leftModels = new ArrayList<>();
        adapter = new CommodityLeftAdapter(this);
        rightModels = new ArrayList<>();
        rightAdapter = new CommodityRightAdapter(this);
        initBack();
        initView();



    }

    private void initBack() {
        //返回
        return_key = findViewById(R.id.return_key);
        return_key.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void initView() {
        leftView = findViewById(R.id.left_rv);
        rightView = findViewById(R.id.right_rv);

        leftView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        rightView.setLayoutManager(new GridLayoutManager(this, 2,GridLayoutManager.VERTICAL,false));


        //一级列表
        shoppDeanPrestion.requestLeftCommodity(new CommodityPresenter.OnLeftResultListener() {
            @Override
            public void onLeftBeanResult(CommdityData[] commdityData) {
                for (int i = 0; i < commdityData.length; i++) {
                    LeftModel Models = new LeftModel();
                    Models.setLeftName(commdityData[i].getName());
                    int e= commdityData[i].getId();
                    leftModels.add(Models);

                    adapter.setBeanList(leftModels);
                    leftView.setAdapter(adapter);

                }


            }
        });



       adapter.setOnItemClickListener(new CommodityLeftAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //默认选中第一个条目
                adapter.setLeftPosition(position);
                adapter.notifyDataSetChanged();

                //根据左边条目id显示右边更灵活
                switch (position){
                    case 0:
                        //先清除后添加

//                        rightModels.clear();
                        rightModels.get(0).getRightName();
                        rightModels.get(0).getRightIcon();
                        rightAdapter.setRightbeanList(rightModels);
                        rightView.setAdapter(rightAdapter);
                        break;
                    case 1:


                        rightModels.get(1).getRightName();
                        rightModels.get(1).getRightIcon();
                        rightAdapter.setRightbeanList(rightModels);
                        rightView.setAdapter(rightAdapter);
                        break;
                    case 2:

//                        rightModels.clear();
                        rightModels.get(2).getRightName();
                        rightModels.get(2).getRightIcon();
                        rightAdapter.setRightbeanList(rightModels);
                        rightView.setAdapter(rightAdapter);
                        break;
                    case 3:

//                        rightModels.clear();
                        rightModels.get(3).getRightName();
                        rightModels.get(3).getRightIcon();
                        rightAdapter.setRightbeanList(rightModels);
                        rightView.setAdapter(rightAdapter);
                        break;

                }

                rightAdapter.notifyDataSetChanged();
            }
        });

    }

    private void rightk(){
        shoppDeanPrestion.requestCommodity(new CommodityPresenter.OnCommodityResultListener() {

            @Override
            public void onCommentBeanResult(CommentBean[] commentBeans) {
                for (int i = 0; i < commentBeans.length; i++) {
                    RightModel model = new RightModel();
                    model.setRightName(commentBeans[i].getName());
//                    model.setRightIcon(commentBeans[i].getIcon());

                    int finalI = i;
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Bitmap bitmap;
                            try {
                                URL url = new URL(commentBeans[finalI].getIcon());
                                Log.d(TAG, "run: "+commentBeans[finalI].getIcon());
                                //创建连接方法
                                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                                //读取超时时长，即链接成功后，服务器返回时间
                                connection.setReadTimeout(5000);
                                //连接超时时长，连接服务器超时时间
                                connection.setConnectTimeout(5000);
                                //设置连接
                                connection.connect();
                                //返回结果码
                                //Log.d(TAG, "1111111111");

                                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK){
                                    InputStream inputStream = connection.getInputStream();//得到数据流
                                    bitmap = BitmapFactory.decodeStream(inputStream);//将流转为bitmap
                                    rightView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                                        @Override
                                        public void onGlobalLayout() {
                                            View view=rightView.getLayoutManager().findViewByPosition(finalI);
                                            im_right=view.findViewById(R.id.im_right);
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    im_right.setImageBitmap(bitmap);
                                                    Log.d(TAG, "runaa: "+bitmap);
                                                }
                                            });
                                            rightView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                                        }
                                    });
                                }else {
                                    Log.d(TAG, "出错了 ");
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    }).start();

                    rightModels.add(model);
                    rightAdapter.setRightbeanList(rightModels);
                    rightView.setAdapter(rightAdapter);
                }
            }
        });
    }

    public void rightke(int position){
      /*  shoppDeanPrestion.requestCommodity(position,new CommodityPresenter.OnCommodityResultListener() {

            @Override
            public void onCommentBeanResult(CommentBean[] commentBeans) {
                for (int i = 0; i < commentBeans.length; i++) {
                    RightModel model = new RightModel();
                    model.setRightName(commentBeans[i].getName());
//                    model.setRightIcon(commentBeans[i].getIcon());

                    int finalI = i;
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Bitmap bitmap;
                            try {
                                URL url = new URL(commentBeans[finalI].getIcon());
                                Log.d(TAG, "run: "+commentBeans[finalI].getIcon());
                                //创建连接方法
                                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                                //读取超时时长，即链接成功后，服务器返回时间
                                connection.setReadTimeout(5000);
                                //连接超时时长，连接服务器超时时间
                                connection.setConnectTimeout(5000);
                                //设置连接
                                connection.connect();
                                //返回结果码
                                //Log.d(TAG, "1111111111");

                                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK){
                                    InputStream inputStream = connection.getInputStream();//得到数据流
                                    bitmap = BitmapFactory.decodeStream(inputStream);//将流转为bitmap
                                    rightView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                                        @Override
                                        public void onGlobalLayout() {
                                            View view=rightView.getLayoutManager().findViewByPosition(finalI);
                                            im_right=view.findViewById(R.id.im_right);
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    im_right.setImageBitmap(bitmap);
                                                    Log.d(TAG, "runaa: "+bitmap);
                                                }
                                            });
                                            rightView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                                        }
                                    });
                                }else {
                                    Log.d(TAG, "出错了 ");
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    }).start();

                    rightModels.add(model);
                    rightAdapter.setRightbeanList(rightModels);
                    rightView.setAdapter(rightAdapter);
                }
            }
        });*/
    }
}



